#include "interface_write.h"

typedef struct position_t
{
  SDL_Rect	position_text;
  SDL_Rect	position_console;
  SDL_Rect	position_adress;
  SDL_Rect	position_key;
  SDL_Rect	position_background;
}		position_t;

typedef struct sdl_t
{
  SDL_Surface		*text;
  char			*line;
  TTF_Font		*font;
}			sdl_t;


static	 void		position_init(position_t	*pos,
				      sdl_t		*sdl)
{
  sdl->text = NULL;
  sdl->line = NULL;
  sdl->font = NULL;

  TTF_Init();
  sdl->line = calloc(50,sizeof(char));

  pos->position_text.x = 20;
  pos->position_text.y = 220;
  pos->position_console.x = 20;
  pos->position_console.y = 550;
  pos->position_adress.x = 385;
  pos->position_adress.y = 115;
  pos->position_key.x = 385;
  pos->position_key.y = 150;
  pos->position_background.x = 0;
  pos->position_background.y = 0;

  sdl->font = TTF_OpenFont("Fixedsys500c.ttf", 14);
}

void	position_cursor(char	*message,
			char	*adress,
			char    *add_key,
			SDL_Surface	*screen)
{
  int		compt = 0, i = 0;
  SDL_Rect	rect_ad, rect_mes, rect_key;
  SDL_Surface	*surface_ad = NULL;
  SDL_Surface	*surface_mes = NULL;
  SDL_Surface	*surface_key = NULL;

  surface_ad = SDL_CreateRGBSurface(SDL_HWSURFACE, 8, 14, 32, 0, 0, 0, 0);
  if (adress)
    rect_ad.x = 385 + strlen(adress) * 8;
  else
    rect_ad.x = 385;
  rect_ad.y = 115;
  SDL_BlitSurface(surface_ad, NULL, screen, &rect_ad);
  SDL_FreeSurface(surface_ad);

  surface_key = SDL_CreateRGBSurface(SDL_HWSURFACE, 8, 14, 32, 0, 0, 0, 0);
  if (add_key)
    rect_key.x = 385 + strlen(add_key) * 8;
  else
    rect_key.x = 385;
  rect_key.y = 150;
  SDL_BlitSurface(surface_key, NULL, screen, &rect_key);
  SDL_FreeSurface(surface_key);

  surface_mes = SDL_CreateRGBSurface(SDL_HWSURFACE, 8, 14, 32, 0, 0, 0, 0);
  if ((message) && (message[0] != '\0'))
  {
    i = strlen(message);
    while (i > 0)
    {
      i = i - 50;
      compt++;
    }
    if (strlen(message) < 50)
      rect_mes.x = 20 + (strlen(message) % 50) * 8;
    else
      rect_mes.x = 20 + ((strlen(message) - 1) % 50) * 8;
    rect_mes.y = 220 + (compt - 1) * 20;
  }
  else
  {
    rect_mes.x = 20;
    rect_mes.y = 220;
  }
  SDL_BlitSurface(surface_mes, NULL, screen, &rect_mes);
  SDL_FreeSurface(surface_mes);
}


void	interface_write(char *message,
			char *adress,
			char *add_key,
			SDL_Surface *background_picture,
			SDL_Surface *screen,
			char	*console)
{
  int i = 0, j = 0;
  position_t*	pos;
  sdl_t*	sdl;
  SDL_Color	black_color = {0,0,0,0};

  sdl = malloc(sizeof (sdl_t));
  pos = malloc(sizeof (position_t));
  TTF_Init();
  position_init(pos, sdl);
  SDL_BlitSurface(background_picture,NULL,screen, &(pos->position_background));
  if (message)
  {
    while (message[i]!='\0')
    {
      if (message[i] != '\n')
      {
	sdl->line[j] = message[i];
	sdl->line[j+1] = '\0';
	i++;
	j++;
      }
      else
      {
	sdl->line[j] = '\0';
	sdl->text = TTF_RenderText_Blended(sdl->font, sdl->line, black_color);
	SDL_BlitSurface(sdl->text,NULL, screen,&(pos->position_text));
	j = 0;
	pos->position_text.y = pos->position_text.y + 20;
      i++;
      }
    }
    sdl->text = TTF_RenderText_Blended(sdl->font, sdl->line, black_color);
    SDL_BlitSurface(sdl->text, NULL, screen, &(pos->position_text));
    sdl->text = TTF_RenderText_Blended(sdl->font, adress, black_color);
    SDL_BlitSurface(sdl->text,NULL, screen, &(pos->position_adress));
    sdl->text = TTF_RenderText_Blended(sdl->font, add_key, black_color);
    SDL_BlitSurface(sdl->text,NULL, screen, &(pos->position_key));
    sdl->text = TTF_RenderText_Blended(sdl->font, console, black_color);
    SDL_BlitSurface(sdl->text,NULL, screen, &(pos->position_console));
  }
  position_cursor(message, adress, add_key, screen);
  SDL_Flip(screen);
  TTF_CloseFont(sdl->font);
  TTF_Quit();
  SDL_FreeSurface(sdl->text);
}
